﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Data.AID
{
    public class Excecao
    {
        public int ExceptionID { get; set; }
        public string ExceptionName { get; set; }
        public string ExceptionLoginName { get; set; }
    }

    public class ExcecaoDAO
    {
        private SPSite varSiteCollection = SPContext.Current.Site;
        private SPWeb varSite = SPContext.Current.Web;

        public List<Excecao> GetAllExceptions()
        {
            List<Excecao> Exceptions = null;

            using (SPSite siteCollection = new SPSite(varSiteCollection.ID))
            {
                using (SPWeb site = siteCollection.OpenWeb(varSite.ID))
                {
                    Exceptions = site.Groups["Exceções"].Users.Cast<SPUser>()
                        .Select(p =>
                            new Excecao
                            {
                                ExceptionID = p.ID,
                                ExceptionName = p.Name,
                                ExceptionLoginName = p.LoginName
                            }).ToList();
                }
            }

            return Exceptions;
        }
        public void DeleteExceptionByUser(SPUser user)
        {
            using (SPSite siteCollection = new SPSite(varSiteCollection.ID))
            {
                using (SPWeb site = siteCollection.OpenWeb(varSite.ID))
                {
                    SPGroup excecao = site.Groups["Exceções"];
                    excecao.RemoveUser(user);
                    excecao.Update();
                }
            }
        }
        public void AddException(SPUser user)
        {
            using (SPSite siteCollection = new SPSite(varSiteCollection.ID))
            {
                using (SPWeb site = siteCollection.OpenWeb(varSite.ID))
                {
                    SPGroup excecao = site.Groups["Exceções"];
                    excecao.AddUser(user);
                    excecao.Update();
                }
            }
        }
    }
}
